import storage from '@/utils/storage'
import config from '../../config.js'
import {api_daikefu,api_zongkefu} from '../../common/api.js'
const app = {
	state: {
		// 当前终端平台
		platform: '',
		serverInfo : config.config,
		toolslist : null,
		wxmpadlist:null,
		// 是否显示小程序广告
		isWxMpAd: false,
		// 视频广告播放最大限制数
		max_video_count: 0,
		zongService:null,//总客服
		/*
		siteaddress: "http://www.baidu.com"
		sitelogo: "http://t.luuqq.com/assets/img/qrcode.png"
		sitename: "总站客服名称"
		sitestatus: "1"
		*/
		
		
		ageService:null,//代理客服
		/*
		apikey: "111"
		createtime: 1649664370
		id: 1
		logo: "http://t.luuqq.com/assets/img/qrcode.png"
		memo: ""
		name: "分站1"
		siteaddress: "http://www.baidu.com"
		sitename: "分站客服1"
		sitestatus: 1
		status: 1
		status_text: "正常"
		updatetime: 1650294203
		*/
	},
	mutations: {
		SET_PLATFORM: (state, value) => {
			state.platform = value
		},
		SET_SHOW_MPAD: (state, value) => {
			state.isWxMpAd = value
		}
	},
	getters: {
		// 全局配置
		platform: state => state.platform,
		wxmpad: state => state.isWxMpAd,
	},
	actions: {
		// 获取后台设置
		getServerSeting({
			state,
			commit
		}, data) {
			
		},
		getService({
			state,
			commit
		}) {
			api_daikefu().then(result => {
				state.ageService = result;
			}).catch(err => {
			});
			
			api_zongkefu().then(result => {
				state.zongService = result;
			}).catch(err => {
			});
		},
		// 检测版本
		updateManager({state,dispatch}){
			// #ifdef APP-PLUS
			dispatch('updateAppManager');
			return;
			// #endif
			// #ifdef MP-WEIXIN
			dispatch('updateWx_MPManager');
			return;
			// #endif
		},
		/* 检测APP版本 */
		updateAppManager({
			state
		}) {
			if (!state.serverInfo) return;
			function toNum(a) {
				var a = a.toString();
				var c = a.split('.');
				var num_place = ["", "0", "00", "000", "0000"],
					r = num_place.reverse();
				for (var i = 0; i < c.length; i++) {
					var len = c[i].length;
					c[i] = r[len] + c[i];
				}
				var res = c.join('');
				return res;
			}
			function cpr_version(nweVersion, currentversion) {
				var _nweVersion = toNum(nweVersion),
					_currentversion = toNum(currentversion);
				let status = 10
				if (_nweVersion > _currentversion) {
					status = 20
				};
						
				return status;
			}
			
			let currentversion = '1.0.0' //当前版本
			let content = ''; // 最新版版本信息
			let downloadUrl = ''; // 下载地址
			let nweVersion = '0'; // 最新版版本
			
			// #ifdef APP-PLUS
				currentversion = plus.runtime.version; //当前版本
			// #endif
			
			if (uni.getSystemInfoSync().platform == 'android') {
				content = state.serverInfo.AndroidText
				downloadUrl = state.serverInfo.AndroidUrl
				nweVersion = state.serverInfo.AndroidVersion
			} else {
				content = state.serverInfo.IOSText
				downloadUrl = state.serverInfo.IOSUrl
				nweVersion = state.serverInfo.IOSVersion
			}
			let status = cpr_version(nweVersion, currentversion);
			let versionInfo = {
				"status": status, //升级标志，10无需升级  20需要升级 
				"content": content, //最新版版本信息
				"url": downloadUrl, //更新包下载地址 
				"currentversion": currentversion, //当前版本
				"edition": nweVersion, //最新版版本
			};
			uni.setStorageSync('versionInfo', versionInfo);
			
			if (status == 20) {
				uni.showModal({
					title: '提示',
					content: '有新的更新，请前往升级！',
					showCancel: false,
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/updataAPP/updataAPP'
							})
						}
					}
				});
			}	
		},
		// 检测小程序版本
		updateWx_MPManager({
			state,
		}, data) {
			const updateManager = uni.getUpdateManager();
			updateManager.onCheckForUpdate(res => {})
			updateManager.onUpdateReady(() => {
				uni.showModal({
					title: '更新提示',
					content: '新版本已经准备好，即将重启应用',
					showCancel: false,
					success(res) {
						if (res.confirm) {
							updateManager.applyUpdate()
						}
					}
				})
			})
			updateManager.onUpdateFailed(() => {
				// 新的版本下载失败
				uni.showModal({
					title: '更新提示',
					content: '新版本下载失败',
					showCancel: false
				})
			})
			
		},
	}
}

export default app
